package zbase;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRMapArrayDataSource;
import net.sf.jasperreports.view.JasperViewer;

public class ReportsDialog extends javax.swing.JDialog {

    public ReportsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setMinimumSize(this.getSize());
        RefreshComboboxes();
        if (Vars.r_m) {
            jRadioButton1.setSelected(Vars.r_jrb1);
            jRadioButton3.setSelected(Vars.r_jrb3);
            if (!Vars.r_jrb3) {
                jLabel1.setEnabled(false);
                jLabel2.setEnabled(false);
                jFormattedTextField1.setEnabled(false);
                jFormattedTextField2.setEnabled(false);
            } else {
                if (Vars.r_ca1 != null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
                    jFormattedTextField1.setText(sdf.format(Vars.r_ca1.getTime()));
                }
                if (Vars.r_ca2 != null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
                    jFormattedTextField2.setText(sdf.format(Vars.r_ca2.getTime()));
                }
            }

            jRadioButton2.setSelected(Vars.r_jrb2);
            jRadioButton4.setSelected(Vars.r_jrb4);
            if (!Vars.r_jrb4) {
                jComboBox1.setEnabled(false);
            } else {
                if (Vars.r_ok != -1) {
                    DefaultComboBoxModel cm = (DefaultComboBoxModel) jComboBox1.getModel();
                    int i = 0;
                    while (i < cm.getSize()) {
                        z_Object vzo = (z_Object) cm.getElementAt(i);
                        if (vzo.getK().equals(Vars.r_ok)) {
                            jComboBox1.setSelectedItem(vzo);
                        }
                        i++;
                    }
                }
            }

            jRadioButton5.setSelected(Vars.r_jrb5);
            jRadioButton6.setSelected(Vars.r_jrb6);
            if (!Vars.r_jrb6) {
                jComboBox2.setEnabled(false);
            } else {
                if (Vars.r_sk != -1) {
                    DefaultComboBoxModel cm = (DefaultComboBoxModel) jComboBox2.getModel();
                    int i = 0;
                    while (i < cm.getSize()) {
                        z_Service vzs = (z_Service) cm.getElementAt(i);
                        if (vzs.getK().equals(Vars.r_sk)) {
                            jComboBox2.setSelectedItem(vzs);
                        }
                        i++;
                    }
                }
            }

            jCheckBox1.setSelected(Vars.r_jcb1);
            jCheckBox2.setSelected(Vars.r_jcb2);
            jCheckBox4.setSelected(Vars.r_jcb4);
            if (Vars.r_t1.equals("")) {
                Vars.r_t1 = Vars.prop.getProperty("ReportSignJob");
            }
            if (Vars.r_t2.equals("")) {
                Vars.r_t2 = Vars.prop.getProperty("ReportSignName");
            }
            if (jCheckBox4.isSelected()) {
                jTextField1.setText(Vars.r_t1);
                jTextField2.setText(Vars.r_t2);
            } else {
                jTextField1.setEnabled(false);
                jTextField2.setEnabled(false);
            }
            jCheckBox5.setSelected(Vars.r_jcb5);

            jCheckBox3.setSelected(true);
        } else {
            jLabel1.setEnabled(false);
            jLabel2.setEnabled(false);
            jFormattedTextField1.setEnabled(false);
            jFormattedTextField2.setEnabled(false);
            jComboBox1.setEnabled(false);
            jComboBox2.setEnabled(false);
            jCheckBox3.setSelected(false);
            jTextField1.setEnabled(false);
            jTextField2.setEnabled(false);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jFormattedTextField1 = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jFormattedTextField2 = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        jComboBox2 = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jCheckBox5 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Подготовка отчета");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Выборка по времени"));

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("За все время");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("За период времени:");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        try {
            jFormattedTextField1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.##.####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFormattedTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jFormattedTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jFormattedTextField1FocusLost(evt);
            }
        });

        jLabel1.setText("по");

        try {
            jFormattedTextField2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.##.####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFormattedTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jFormattedTextField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jFormattedTextField2FocusLost(evt);
            }
        });

        jLabel2.setText("с");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jRadioButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton3)
                    .addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Выборка по объекту"));

        buttonGroup2.add(jRadioButton2);
        jRadioButton2.setSelected(true);
        jRadioButton2.setText("Все объекты");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        buttonGroup2.add(jRadioButton4);
        jRadioButton4.setText("Объект:     ");
        jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton4ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton2)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jRadioButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, 0, 513, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton4)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jButton1.setText("Показать отчет");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Отмена");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Выборка по службе"));

        buttonGroup3.add(jRadioButton5);
        jRadioButton5.setSelected(true);
        jRadioButton5.setText("Все службы");
        jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton5ActionPerformed(evt);
            }
        });

        buttonGroup3.add(jRadioButton6);
        jRadioButton6.setText("Служба:");
        jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton6ActionPerformed(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton5)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jRadioButton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox2, 0, 530, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton6)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Другие параметры"));

        jCheckBox1.setText("Показать только невыполненные заявки");

        jCheckBox2.setText("Показать только заявки с отметкой об особом внимании");

        jCheckBox4.setText("Подпись на последней странице");
        jCheckBox4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox4ActionPerformed(evt);
            }
        });

        jTextField2.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jCheckBox5.setText("Разбить службам");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox1)
                            .addComponent(jCheckBox2)
                            .addComponent(jCheckBox5)
                            .addComponent(jCheckBox4)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jCheckBox3.setSelected(true);
        jCheckBox3.setText("Запомнить");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jCheckBox3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 313, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1)
                    .addComponent(jCheckBox3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private Boolean ValidateData(String dtstr, Boolean strogo) {
        try {
            int day = Integer.parseInt(dtstr.substring(0, 2));
            if (day > 31 || day == 0) {
                return false;
            }
            int month = Integer.parseInt(dtstr.substring(3, 5));
            month--;
            if (month > 11 || month == -1) {
                return false;
            }
            int year = Integer.parseInt(dtstr.substring(6, 10));
        } catch (NumberFormatException ex) {
            if (strogo) {
                return false;
            }
        }
        return true;
    }

    private void RefreshComboboxes() {
        DB db = new DB();
        DefaultComboBoxModel cm = (DefaultComboBoxModel) jComboBox1.getModel();
        cm.removeAllElements();
        ResultSet rs = db.getResultSet("SELECT k, objname FROM objects WHERE hide = 0 ORDER BY objname");
        try {
            while (rs.next()) {
                cm.addElement(new z_Object(rs.getInt("k"), Vars.getStringFromHex(rs.getString("objname"))));
            }
        } catch (SQLException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NullPointerException ex) {
            Logger.getLogger(ObjectsDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.closeResultSet();

        DefaultComboBoxModel cm1 = (DefaultComboBoxModel) jComboBox2.getModel();
        cm1.removeAllElements();
        ResultSet rs1 = db.getResultSet("SELECT k, servicename FROM services WHERE hide = 0 ORDER BY servicename");
        try {
            while (rs1.next()) {
                cm1.addElement(new z_Service(rs1.getInt("k"), Vars.getStringFromHex(rs1.getString("servicename"))));
            }
        } catch (SQLException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NullPointerException ex) {
            Logger.getLogger(ObjectsDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.closeResultSet();
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        jLabel1.setEnabled(true);
        jLabel2.setEnabled(true);
        jFormattedTextField1.setEnabled(true);
        jFormattedTextField2.setEnabled(true);
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        jLabel1.setEnabled(false);
        jLabel2.setEnabled(false);
        jFormattedTextField1.setEnabled(false);
        jFormattedTextField2.setEnabled(false);
        jFormattedTextField1.setValue(null);
        jFormattedTextField2.setValue(null);
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jFormattedTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField1FocusLost
        if (!ValidateData(jFormattedTextField1.getText(), false)) {
            JOptionPane.showMessageDialog(null, "Ошибка в дате.", "Внимание!", JOptionPane.WARNING_MESSAGE);
            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
            Calendar ca = Calendar.getInstance();
            ca.add(Calendar.DAY_OF_YEAR, -1);
            jFormattedTextField1.setText(sdf.format(ca.getTime()));
        }
    }//GEN-LAST:event_jFormattedTextField1FocusLost

    private void jFormattedTextField2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jFormattedTextField2FocusLost
        if (!ValidateData(jFormattedTextField2.getText(), false)) {
            JOptionPane.showMessageDialog(null, "Ошибка в дате.", "Внимание!", JOptionPane.WARNING_MESSAGE);
            SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
            jFormattedTextField2.setText(sdf.format(Calendar.getInstance().getTime()));
        }
    }//GEN-LAST:event_jFormattedTextField2FocusLost

    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton4ActionPerformed
        jComboBox1.setEnabled(true);
    }//GEN-LAST:event_jRadioButton4ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        jComboBox1.setEnabled(false);
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton6ActionPerformed
        jComboBox2.setEnabled(true);
    }//GEN-LAST:event_jRadioButton6ActionPerformed

    private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton5ActionPerformed
        jComboBox2.setEnabled(false);
}//GEN-LAST:event_jRadioButton5ActionPerformed

    @SuppressWarnings("unchecked")
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Calendar dt1 = null;
        Calendar dt2 = null;
        if (jRadioButton3.isSelected()) {
            if (!ValidateData(jFormattedTextField1.getText(), true)) {
                JOptionPane.showMessageDialog(null, "Ошибка в дате.", "Внимание!", JOptionPane.WARNING_MESSAGE);
                jFormattedTextField1.requestFocus();
                return;
            }
            if (!ValidateData(jFormattedTextField2.getText(), true)) {
                JOptionPane.showMessageDialog(null, "Ошибка в дате.", "Внимание!", JOptionPane.WARNING_MESSAGE);
                jFormattedTextField2.requestFocus();
                return;
            }
            int Year1 = Integer.parseInt(jFormattedTextField1.getText().substring(6, 10));
            int Month1 = Integer.parseInt(jFormattedTextField1.getText().substring(3, 5));
            Month1--;
            int Day1 = Integer.parseInt(jFormattedTextField1.getText().substring(0, 2));
            dt1 = new GregorianCalendar(Year1, Month1, Day1);
            int Year2 = Integer.parseInt(jFormattedTextField2.getText().substring(6, 10));
            int Month2 = Integer.parseInt(jFormattedTextField2.getText().substring(3, 5));
            Month2--;
            int Day2 = Integer.parseInt(jFormattedTextField2.getText().substring(0, 2));
            dt2 = new GregorianCalendar(Year2, Month2, Day2);
        }

        Vars.r_m = jCheckBox3.isSelected();
        Vars.r_ca1 = dt1;
        Vars.r_ca2 = dt2;
        Vars.r_jcb1 = jCheckBox1.isSelected();
        Vars.r_jcb2 = jCheckBox2.isSelected();
        Vars.r_jcb4 = jCheckBox4.isSelected();
        Vars.r_jcb5 = jCheckBox5.isSelected();
        Vars.r_jrb1 = jRadioButton1.isSelected();
        Vars.r_jrb2 = jRadioButton2.isSelected();
        Vars.r_jrb3 = jRadioButton3.isSelected();
        Vars.r_jrb4 = jRadioButton4.isSelected();
        Vars.r_jrb5 = jRadioButton5.isSelected();
        Vars.r_jrb6 = jRadioButton6.isSelected();
        z_Object vzo = (z_Object) jComboBox1.getSelectedItem();
        if (vzo != null) {
            Vars.r_ok = vzo.getK();
        } else {
            Vars.r_ok = -1;
        }
        z_Service vzs = (z_Service) jComboBox2.getSelectedItem();
        if (vzs != null) {
            Vars.r_sk = vzs.getK();
        } else {
            Vars.r_sk = -1;
        }
        if (jCheckBox4.isSelected()) {
            Vars.r_t1 = jTextField1.getText();
            Vars.r_t2 = jTextField2.getText();
        } else {
            Vars.r_t1 = Vars.prop.getProperty("ReportSignJob");
            Vars.r_t2 = Vars.prop.getProperty("ReportSignName");
        }

        String sql = "SELECT zmain.k, zmain.zdatetime, objects.objname, zmain.ztext, services.servicename, zmain.zprim, zmain.zalert, zmain.zdone, zmain.zdonedt, zmain.hide FROM zmain LEFT OUTER JOIN services ON zmain.zservice = services.k LEFT OUTER JOIN objects ON zmain.zobject = objects.k WHERE zmain.hide = 0";
        if (jRadioButton3.isSelected()) {
            Integer dt1_y = dt1.get(Calendar.YEAR);
            Integer dt1_m = dt1.get(Calendar.MONTH);
            dt1_m++;
            Integer dt1_d = dt1.get(Calendar.DAY_OF_MONTH);
            String dt1s_y = dt1_y.toString();
            String dt1s_m = dt1_m.toString();
            if (dt1s_m.length() < 2) {
                dt1s_m = "0" + dt1s_m;
            }
            String dt1s_d = dt1_d.toString();
            if (dt1s_d.length() < 2) {
                dt1s_d = "0" + dt1s_d;
            }
            Integer dt2_y = dt2.get(Calendar.YEAR);
            Integer dt2_m = dt2.get(Calendar.MONTH);
            dt2_m++;
            Integer dt2_d = dt2.get(Calendar.DAY_OF_MONTH);
            String dt2s_y = dt2_y.toString();
            String dt2s_m = dt2_m.toString();
            if (dt2s_m.length() < 2) {
                dt2s_m = "0" + dt2s_m;
            }
            String dt2s_d = dt2_d.toString();
            if (dt2s_d.length() < 2) {
                dt2s_d = "0" + dt2s_d;
            }
            String dts = " AND (zdatetime BETWEEN '" + dt1s_y + "-" + dt1s_m + "-" + dt1s_d + " 00:00:00.000' AND '" + dt2s_y + "-" + dt2s_m + "-" + dt2s_d + " 23:59:59.000')";
            sql = sql + dts;
        }
        if (jRadioButton4.isSelected()) {
            z_Object tzo = (z_Object) jComboBox1.getSelectedItem();
            if (tzo != null) {
                String obs = " AND (zobject = " + tzo.getK().toString() + ")";
                sql = sql + obs;
            }
        }
        if (jRadioButton6.isSelected()) {
            z_Service tzs = (z_Service) jComboBox2.getSelectedItem();
            if (tzs != null) {
                String ses = " AND (zservice = " + tzs.getK().toString() + ")";
                sql = sql + ses;
            }
        }
        if (jCheckBox1.isSelected()) {
            sql = sql + " AND (zdone = 0)";
        }
        if (jCheckBox2.isSelected()) {
            sql = sql + " AND (zalert = 1)";
        }

        // Show report
        WaitingWindow ww = new WaitingWindow();

        DB db = new DB();

        ArrayList<Map> lrr = new ArrayList<Map>();
        SimpleDateFormat sdt = new SimpleDateFormat("dd.MM.yyyy HH:mm");

        ResultSet rs = db.getResultSet(sql);
        try {
            Integer rownum = 0;
            while (rs.next()) {
                Map hm = new HashMap();

                Integer k = rs.getInt("k");
                rownum++;
                hm.put("rownum", rownum.toString());
                hm.put("z_number", k.toString());
                Calendar zdatetime = new GregorianCalendar();
                zdatetime.setTimeInMillis(rs.getTimestamp("zdatetime").getTime());
                String szdt = sdt.format(zdatetime.getTime());
                hm.put("z_datetime", szdt);
                hm.put("z_object", Vars.getStringFromHex(rs.getString("objname")));
                hm.put("z_service", Vars.getStringFromHex(rs.getString("servicename")));
                hm.put("z_text", Vars.getStringFromHex(rs.getString("ztext")));
                hm.put("z_prim", Vars.getStringFromHex(rs.getString("zprim")));
                if (rs.getBoolean("zdone")) {
                    hm.put("z_done", "Выполнено");
                } else {
                    hm.put("z_done", "");
                }

                lrr.add(hm);
            }
        } catch (SQLException ex) {
            System.out.println(ex.toString());
        }
        db.closeResultSet();

        Map[] reportRows = new Map[lrr.size()];
        reportRows = lrr.toArray(reportRows);

        JRMapArrayDataSource dataSource = new JRMapArrayDataSource(reportRows);
        JasperPrint jp = null;
        try {
            Map pm = new HashMap();
            SimpleDateFormat sdtd = new SimpleDateFormat("dd.MM.yyyy");
            if (jRadioButton1.isSelected() || dt1 == null || dt2 == null) {
                pm.put("report_title", "Отчет по заявкам");
            } else {
                pm.put("report_title", "Отчет по заявкам\nс " + sdtd.format(dt1.getTime()) + " по " + sdtd.format(dt2.getTime()));
            }
            pm.put("sign_job", jTextField1.getText());
            pm.put("sign_name", jTextField2.getText());
            if (jCheckBox5.isSelected()) {
                jp = JasperFillManager.fillReport("reportrs.jasper", pm, dataSource);
            } else {
                jp = JasperFillManager.fillReport("report.jasper", pm, dataSource);
            }
        } catch (JRException ex) {
            System.out.println(ex.toString());
            ww.close();
            return;
        }

        dispose();
        ww.close();

        if (jp.getPages().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Соответствующих запросу заявок не найдено.", "Информация", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        JasperViewer jv = new JasperViewer(jp, false);
        jv.setTitle("Отчет");
        jv.setLocationRelativeTo(null);
        jv.setVisible(true);
        jv.setFitPageZoomRatio();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jCheckBox4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox4ActionPerformed
        if (jCheckBox4.isSelected()) {
            jTextField1.setEnabled(true);
            jTextField2.setEnabled(true);
            jTextField1.setText(Vars.r_t1);
            jTextField2.setText(Vars.r_t2);
        } else {
            jTextField1.setEnabled(false);
            jTextField2.setEnabled(false);
            jTextField1.setText("");
            jTextField2.setText("");
        }
    }//GEN-LAST:event_jCheckBox4ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JFormattedTextField jFormattedTextField1;
    private javax.swing.JFormattedTextField jFormattedTextField2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
